package org.ftz;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.ftz.control.DBUtil;
import org.ftz.control.util;
import org.ftz.pojo.Problem;
import org.ftz.pojo.Status;
import org.ftz.pojo.StuShow;
import org.ftz.pojo.Testcase;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.tools.Tool;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
public class MainFrame {
    public JPanel panel1;
    public JPanel panel2;
    public JPanel panel3;
    public JPanel panel4s;
    public JPanel panel5;
    public JPanel panel6;
    public JPanel panel7;
    public Container c;
    public JButton cleanBtn;
    public JButton runBtn;
    public JTextArea codeArea;
    public JTextArea inputArea;
    public JTextArea outputArea;
    public JFrame jf;
    public JButton jb;
    public JTextArea jtf;
    public JPanel panel4;
    public JComboBox<String> jComboBox;
    public JButton clearAll;
    public JTabbedPane tabbedPane;
    public JButton jButtonSelect;
    public JButton jButtonSend;
    public JTextArea jTextAreaFile;
    public JPanel panelOJ;
    public DefaultTableModel model;
    public DefaultTableModel model2;
    public JTable jTable;
    public JTable jTable2;
    Page page=null;
    Page page2=null;
    Page page3=null;
    public JLabel show;
    public JLabel show2;
    public JFrame detaliFrame;
    int flag=0;
    public JButton next;
    public void mainFrame1(){
        jf = new JFrame("程序设计远程协助系统");
        jf.setSize(1000, 700);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        c= jf.getContentPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4s=new JPanel();
        panel5=new JPanel();
        panel6=new JPanel();
        panel7=new JPanel();
        panelOJ=new JPanel();
        panel1.setBackground(Color.gray);
        Box allBox1 = Box.createHorizontalBox(); //界面一   在线测评  左侧本文框输入代码，右边上侧输入，右侧下部分显示运行结果
        JPanel lPanel = new JPanel();
        Box lBox = Box.createVerticalBox();
        Box topLabel=Box.createHorizontalBox();
        jComboBox=new JComboBox<>();
        jComboBox.addItem("c/c++在线工具");
        jComboBox.addItem("java在线工具");
        JLabel codeLabel = new JLabel("代码输入区域");
        ImageIcon imageIcon = new ImageIcon("src\\main\\java\\org\\ftz\\images\\清空 (1).png");
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(16,16,Image.SCALE_DEFAULT));
        clearAll=new JButton("清空所有");
        clearAll.setIcon(imageIcon);
        topLabel.add(jComboBox);
        topLabel.add(Box.createHorizontalStrut(100));
        topLabel.add(codeLabel);
        topLabel.add(Box.createHorizontalStrut(100));
        topLabel.add(clearAll);
        codeArea = new JTextArea(25,40);   //代码框
        codeArea.setLineWrap(true);
        JScrollPane codeScrollPane = new JScrollPane(codeArea,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        Box btnBox = Box.createHorizontalBox();  //两个按钮： 清空和运行
        cleanBtn = new JButton("清空");
        cleanBtn.setIcon(imageIcon);
        ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\发送.png");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(24,24,Image.SCALE_DEFAULT));
        runBtn = new JButton("点击运行");
        runBtn.setIcon(imageIcon1);
        btnBox.add(cleanBtn);
        btnBox.add(Box.createHorizontalStrut(50));
        btnBox.add(runBtn);
        lBox.add(topLabel); //垂直布局
        lBox.add(codeScrollPane);
        lBox.add(btnBox);
        lPanel.add(lBox);
        Box rBox = Box.createVerticalBox(); //右侧面板  上侧运行结果、下侧输入内容
        JPanel rPanel = new JPanel();
        inputArea = new JTextArea(8, 20);
        outputArea= new JTextArea(10, 20);
        JLabel inputLabel = new JLabel("标准输入");
        JLabel outputLabel = new JLabel("运行结果");
        JScrollPane inputScrollPane = new JScrollPane(inputArea,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        inputArea.setLineWrap(true);
        JScrollPane outputScrollPane = new JScrollPane(outputArea,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outputArea.setLineWrap(true);
        rBox.add(Box.createVerticalStrut(10));
        rBox.add(inputLabel);
        rBox.add(Box.createVerticalStrut(5));
        rBox.add(inputScrollPane);
        rBox.add(Box.createVerticalStrut(10));
        rBox.add(outputLabel);
        rBox.add(outputScrollPane);
        rPanel.add(rBox);
        allBox1.add(lPanel);
        allBox1.add(rPanel);
        panel1.add(allBox1);
    }
    public void mainFrame2(){
        panel3.setBounds(0, 0, 850, 550); //聊天界面实现
        panel3.setLayout(null);
        panel3.setVisible(true);
        JScrollPane jsp = new JScrollPane();
        jsp.setBounds(0, 0, 850, 450);
        panel4 = new JPanel(null);
        panel4.setBackground(new Color(241,241,241));
        panel4.setVisible(true);
        jsp.getViewport().add(panel4);
        jsp.validate();
        panel3.add(jsp);
        JPanel jPanel5=new JPanel();
        Box box=Box.createHorizontalBox();
        jTextAreaFile=new JTextArea(4,30);
        jButtonSelect=new JButton("选择");
        ImageIcon imageIcon6 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\文件夹.png");
        imageIcon6.setImage(imageIcon6.getImage().getScaledInstance(14,14,Image.SCALE_DEFAULT));
        jButtonSelect.setIcon(imageIcon6);
        jButtonSend=new JButton("传输文件");
        Box box1=Box.createVerticalBox();
        box1.add(jButtonSelect);
        box1.add(Box.createVerticalStrut(10));
        box1.add(jButtonSend);
        jtf=new JTextArea(4,30);
        jb=new JButton("发送");
        ImageIcon imageIcon7 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\发送消息.png");
        imageIcon7.setImage(imageIcon7.getImage().getScaledInstance(18,18,Image.SCALE_DEFAULT));
        jb.setIcon(imageIcon7);
        box.add(jTextAreaFile);
        box.add(Box.createHorizontalStrut(10));
        box.add(box1);
        box.add(Box.createHorizontalStrut(40));
        box.add(jtf);
        box.add(Box.createHorizontalStrut(10));
        box.add(jb);
        jPanel5.add(box);
        jPanel5.setBounds(0,450,860,100);
        jPanel5.setVisible(true);
        panel3.add(jPanel5);
        tabbedPane = new JTabbedPane();
        JLabel tab1 = new JLabel();
        JLabel tab2 = new JLabel();
        JLabel tab3 = new JLabel();
        JLabel tab4=new JLabel();
        panel1.add(tab1);
        panel2.add(tab2);
        panel3.add(tab3);
        panelOJ.add(tab4);
        panelOJ.setLayout(null);
        JLabel label=new JLabel("Problem List");
        label.setFont(new Font("楷体",Font.BOLD,20));
        label.setBounds(10,0,140,40);
        label.setBackground(Color.GREEN);
        panelOJ.add(label);
        JButton add=new JButton("状态");
        ImageIcon imageIcon=new ImageIcon("src\\main\\java\\org\\ftz\\images\\电波心电图.png");
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
        add.setIcon(imageIcon);
        add.setBounds(700,5,80,30);
        panelOJ.add(add);
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame status=new JFrame("Status");
                status.setResizable(false);
                status.setLocationRelativeTo(null);
                status.setSize(980,670);
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                status.setLocation((d.width-status.getSize().width)/2,(d.height-status.getSize().height)/2);
                JPanel panel=new JPanel(null);
                JLabel label=new JLabel("Status");
                label.setFont(new Font("楷体",Font.BOLD,20));
                label.setBounds(10,0,140,40);
                label.setBackground(Color.GREEN);
                panel.add(label);
                JLabel label1=new JLabel();
                ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\关.png");
                imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(52,26,Image.SCALE_DEFAULT));
                label1.setIcon(imageIcon1);
                label1.setBounds(740,5,80,30);
                panel.add(label1);
                label1.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getButton()==MouseEvent.BUTTON1){
                            if(flag==0){
                                flag=1;
                                System.out.println("当前是所有");
                                ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\开.png");
                                imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(52,26,Image.SCALE_DEFAULT));
                                label1.setIcon(imageIcon1);
                                page2 = new Page();
                                page3 = new Page();
                                int column = getColumn3();
                                System.out.println("page3:"+column);
                                if(column%page3.getPageSize()==0){
                                    page3.setLastPage(column/page3.getPageSize());
                                }else {
                                    page3.setLastPage(column/page3.getPageSize()+1);
                                }
                                showTable3(1);
                                System.out.println("page3.getLastPage():"+page3.getLastPage());
                                show2.setText("当前是第1页/共"+page3.getLastPage()+"页");
                            }else if(flag==1) {
                                flag=0;
                                ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\关.png");
                                imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(52,26,Image.SCALE_DEFAULT));
                                label1.setIcon(imageIcon1);
                                System.out.println("我的");
                                int column = getColumn2();
                                if(column%page2.getPageSize()==0){
                                    page2.setLastPage(column/page2.getPageSize());
                                }else {
                                    page2.setLastPage(column/page2.getPageSize()+1);
                                }
                                showTable2(1);
                            }
                        }
                    }
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                });
                String[] columnNames={"When","IP","Status","Problem","Language"};
                model2=new DefaultTableModel(columnNames,0);
                jTable2 = new JTable(model2){
                    @Override
                    public boolean isCellEditable(int row, int column) {
                        return false;
                    }
                };
                JTableHeader jTableHeader=jTable2.getTableHeader();
                jTableHeader.setFont(new Font("微软雅黑",Font.PLAIN,16));
                JScrollPane jScrollPane=new JScrollPane();
                jScrollPane.setViewportView(jTable2);
                jTable2.setRowHeight(40);
                jTable2.setFont(new Font("新宋体",Font.PLAIN,16));
                jTable2.getColumnModel().getColumn(0).setPreferredWidth(220);
                jTable2.getColumnModel().getColumn(1).setPreferredWidth(160);
                jTable2.getColumnModel().getColumn(2).setPreferredWidth(140);
                jTable2.getColumnModel().getColumn(3).setPreferredWidth(130);
                jTable2.getColumnModel().getColumn(4).setPreferredWidth(130);
                jTable2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                jTable2.getColumnModel().getColumn(2).setCellRenderer(new TableCellRendererButton());
                jScrollPane.setBounds(10,40,800,455);
                panel.add(jScrollPane);
                page2 = new Page();
                int column = getColumn2();
                if(column%page2.getPageSize()==0){
                    page2.setLastPage(column/page2.getPageSize());
                }else {
                    page2.setLastPage(column/page2.getPageSize()+1);
                }
                showTable2(1);
                JButton fitst=new JButton("首页");
                fitst.setBounds(100,500,60,30);
                fitst.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(flag==0){
                            show2.setText("当前是第1页/共"+page2.getLastPage()+"页");
                            showTable2(1);
                        }else if(flag==1){
                            show2.setText("当前是第1页/共"+page3.getLastPage()+"页");
                            showTable3(1);
                        }
                    }
                });
                JButton prex=new JButton("上一页");
                prex.setBounds(200,500,60,30);
                prex.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(flag==0){
                            if(page2.getCurrentPage()<=1){
                                page2.setCurrentPage(2);
                                show2.setText("当前是第1页/共"+page2.getLastPage()+"页");
                            }
                            showTable2(page2.getCurrentPage()-1);
                            show2.setText("当前是第"+(page2.getCurrentPage())+"页/共"+page2.getLastPage()+"页");
                        }else if(flag==1){
                            if(page3.getCurrentPage()<=1){
                                page3.setCurrentPage(2);
                                show2.setText("当前是第1页/共"+page3.getLastPage()+"页");
                            }
                            showTable3(page3.getCurrentPage()-1);
                            show2.setText("当前是第"+(page3.getCurrentPage())+"页/共"+page3.getLastPage()+"页");
                        }
                    }
                });
                next=new JButton("下一页");
                next.setBounds(300,500,60,30);
                next.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(flag);
                        if(flag==0){
                            if(page2.getCurrentPage()<page2.getLastPage()){
                                showTable2(page2.getCurrentPage()+1);
                                show2.setText("当前是第"+(page2.getCurrentPage())+"页/共"+page2.getLastPage()+"页");
                            }else {
                                show2.setText("当前是第"+page2.getLastPage()+"页/共"+page2.getLastPage()+"页");
                                showTable2(page2.getLastPage());
                            }
                        }else if(flag==1){
                            if(page3.getCurrentPage()<page3.getLastPage()){
                                showTable3(page3.getCurrentPage()+1);
                                show2.setText("当前是第"+(page3.getCurrentPage())+"页/共"+page3.getLastPage()+"页");
                            }else {
                                show2.setText("当前是第"+page3.getLastPage()+"页/共"+page3.getLastPage()+"页");
                                showTable3(page3.getLastPage());
                            }
                        }

                    }
                });
                JButton last=new JButton("尾页");
                last.setBounds(400,500,60,30);
                last.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(flag==0){
                            show2.setText("当前是第"+page2.getLastPage()+"页/共"+page2.getLastPage()+"页");
                            showTable2(page2.getLastPage());
                        }else if(flag==1){
                            show2.setText("当前是第"+page3.getLastPage()+"页/共"+page3.getLastPage()+"页");
                            showTable3(page3.getLastPage());
                        }

                    }
                });
                show2=new JLabel("当前是第1页/共"+page2.getLastPage()+"页");
                show2.setBounds(500,500,120,30);
                panel.add(fitst);
                panel.add(prex);
                panel.add(next);
                panel.add(last);
                panel.add(show2);
                status.setContentPane(panel);
                status.setVisible(true);
            }
        });
        String[] columnNames={"Id","Title","Level","Total","AC Rate"};
        model=new DefaultTableModel(columnNames,0);
        jTable = new JTable(model){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        JTableHeader jTableHeader=jTable.getTableHeader();
        jTableHeader.setFont(new Font("微软雅黑",Font.PLAIN,16));
        JScrollPane jScrollPane=new JScrollPane();
        jScrollPane.setViewportView(jTable);
        jTable.setRowHeight(40);
        jTable.setFont(new Font("新宋体",Font.PLAIN,16));
        jTable.getColumnModel().getColumn(0).setPreferredWidth(95);
        jTable.getColumnModel().getColumn(1).setPreferredWidth(400);
        jTable.getColumnModel().getColumn(2).setPreferredWidth(95);
        jTable.getColumnModel().getColumn(3).setPreferredWidth(95);
        jTable.getColumnModel().getColumn(4).setPreferredWidth(95);
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = jTable.getSelectedRow();
                int col=jTable.getSelectedColumn();
                int id=(int)jTable.getValueAt(row,0);
                if(col==0){
                    System.out.println(id);
                    Problem problem = selectBId(id);
                    System.out.println(problem.getDescription());
                    detaliFrame =new JFrame();
                    detaliFrame.setTitle("题目详情("+id+")");
                    detaliFrame.setResizable(false);
                    detaliFrame.setSize(950,650);
                    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                    detaliFrame.setLocation((d.width-detaliFrame.getSize().width)/2,(d.height-detaliFrame.getSize().height)/2);
                    JPanel panel=new JPanel(null);
                    JTextArea textArea=new JTextArea(10,10);
                    JScrollPane scrollPane1=new JScrollPane(textArea);
                    textArea.setWrapStyleWord(true);
                    textArea.setFont(new Font("微软雅黑",Font.BOLD,16));
                    textArea.setText(problem.getDescription());
                    textArea.setEditable(false);
                    scrollPane1.setBounds(10,10,860,250);
                    JLabel language=new JLabel("Language:");
                    language.setFont(new Font("微软雅黑",Font.BOLD,18));
                    language.setBounds(20,265,100,30);
                    JComboBox jComboBox2=new JComboBox();
                    jComboBox2.addItem("C");
                    jComboBox2.addItem("C++");
                    jComboBox2.addItem("Java");
                    jComboBox2.setBounds(120,265,100,30);
                    JButton submit=new JButton("Submit");
                    submit.setBounds(280,265,95,30);
                    submit.setFont(new Font("微软雅黑",Font.PLAIN,16));
                    ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\写.png");
                    imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(14,14,Image.SCALE_DEFAULT));
                    submit.setIcon(imageIcon1);
                    JButton clear=new JButton("Clear");
                    clear.setBounds(400,265,80,30);
                    clear.setFont(new Font("微软雅黑",Font.PLAIN,16));
                    ImageIcon imageIcon2 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\清空 (2).png");
                    imageIcon2.setImage(imageIcon2.getImage().getScaledInstance(16,16,Image.SCALE_DEFAULT));
                    clear.setIcon(imageIcon2);
                    panel.add(clear);
                    JLabel shows=new JLabel("Pending");
                    shows.setOpaque(true);
                    shows.setBackground(new Color(255,153,0));
                    shows.setForeground(Color.WHITE);
                    shows.setFont(new Font("微软雅黑",Font.PLAIN,16));
                    Border border = shows.getBorder();
                    Border emptyBorder = new EmptyBorder(10, 10, 10, 10);
                    shows.setBorder(new CompoundBorder(border,emptyBorder));
                    shows.setBounds(600,265,93,30);
                    shows.setVisible(false);
                    panel.add(shows);
                    JPanel cp=new JPanel();
                    cp.setBounds(10,300,860,270);
                    RSyntaxTextArea textArea1 = new RSyntaxTextArea(15, 112);
                    textArea1.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CPLUSPLUS);
                    textArea1.setCodeFoldingEnabled(true);
                    RTextScrollPane sp = new RTextScrollPane(textArea1);
                    cp.add(sp);
                    panel.add(scrollPane1);
                    panel.add(language);
                    panel.add(submit);
                    panel.add(jComboBox2);
                    panel.add(cp);
                    detaliFrame.setContentPane(panel);
                    detaliFrame.setVisible(true);
                    clear.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            textArea1.setText("");
                            shows.setVisible(false);
                        }
                    });
                    submit.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String code=textArea1.getText();
                            System.out.println(jComboBox2.getSelectedIndex());
                            System.out.println(textArea1.getText());
                            if(jComboBox2.getSelectedIndex()==0||jComboBox2.getSelectedIndex()==1){
                                String ip = "";
                                try {
                                    ip= InetAddress.getLocalHost().getHostAddress();
                                } catch (UnknownHostException unknownHostException) {
                                    unknownHostException.printStackTrace();
                                }
                                String out="";
                                shows.setVisible(true);
                                org.ftz.JudgeOnline.question.ExecuteCLangService executeCLangService=new org.ftz.JudgeOnline.question.ExecuteCLangService();
                                out = executeCLangService.runCLangCode(code,id);
                                System.out.println("out："+out);
                                String showTag = "";
                                if(out.equals("成功")){
                                    showTag="Accepted";
                                    shows.setBackground(new Color(25,190,107));
                                    shows.setText(showTag);
                                    shows.setVisible(true);
                                }else if(out.equals("答案错误")){
                                    showTag="Wrong Answer";
                                    shows.setText(showTag);
                                    shows.setBackground(new Color(237,63,20));
                                    shows.setSize(134,30);
                                    shows.setVisible(true);
                                }else if(out.equals("编译出错")){
                                    showTag="Compile Error";
                                    shows.setText(showTag);
                                    shows.setBackground(new Color(255,153,0));
                                    shows.setSize(130,30);
                                    shows.setVisible(true);
                                }
                                Status status = new Status(ip,id,showTag,jComboBox2.getSelectedItem().toString(),new Date());
                                try {
                                    insertStatus(status);
                                } catch (UnknownHostException unknownHostException) {
                                    unknownHostException.printStackTrace();
                                }
                                System.out.println("showTag："+showTag);
                            }
                        }
                    });
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        DefaultTableCellRenderer trc=new DefaultTableCellRenderer(){
            public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column){
                Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(table.getValueAt(row,column).equals("Low")&&cell.isBackgroundSet()){
                    cell.setForeground(new Color(25,190,107));
                    cell.setFont(new Font("微软雅黑",Font.BOLD,16));
                }else if(table.getValueAt(row, column).equals("High") && cell.isBackgroundSet()){
                    cell.setForeground(new Color(255,153,0));
                    cell.setFont(new Font("微软雅黑",Font.BOLD,16));
                }
                else if(table.getValueAt(row, column).equals("Mid") && cell.isBackgroundSet()) {
                    cell.setForeground(new Color(45,140,240));
                    cell.setFont(new Font("微软雅黑",Font.BOLD,16));
                }
                return cell;
            }
        };
        jTable.getColumn("Level").setCellRenderer(trc);
        jScrollPane.setBounds(10,40,800,455);
        panelOJ.add(jScrollPane);
        page = new Page();
        int column = getColumn();
        if(column%page.getPageSize()==0){
            page.setLastPage(column/page.getPageSize());
        }else {
            page.setLastPage(column/page.getPageSize()+1);
        }
        showTable(1);
        JButton fitst=new JButton("首页");
        fitst.setBounds(100,500,60,30);
        fitst.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                show.setText("当前是第1页/共"+page.getLastPage()+"页");
                showTable(1);
            }
        });
        JButton prex=new JButton("上一页");
        prex.setBounds(200,500,60,30);
        prex.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(page.getCurrentPage()<=1){
                    page.setCurrentPage(2);
                    show.setText("当前是第1页/共"+page.getLastPage()+"页");
                }
                showTable(page.getCurrentPage()-1);
                show.setText("当前是第"+(page.getCurrentPage())+"页/共"+page.getLastPage()+"页");
            }
        });
        JButton next=new JButton("下一页");
        next.setBounds(300,500,60,30);
        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(page.getCurrentPage()<page.getLastPage()){
                    showTable(page.getCurrentPage()+1);
                    show.setText("当前是第"+(page.getCurrentPage())+"页/共"+page.getLastPage()+"页");
                }else {
                    show.setText("当前是第"+page.getLastPage()+"页/共"+page.getLastPage()+"页");
                    showTable(page.getLastPage());
                }
            }
        });
        JButton last=new JButton("尾页");
        last.setBounds(400,500,60,30);
        last.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                show.setText("当前是第"+page.getLastPage()+"页/共"+page.getLastPage()+"页");
                showTable(page.getLastPage());
            }
        });
        show=new JLabel("当前是第1页/共"+page.getLastPage()+"页");
        show.setBounds(500,500,120,30);
        panelOJ.add(fitst);
        panelOJ.add(prex);
        panelOJ.add(next);
        panelOJ.add(last);
        panelOJ.add(show);
        panel6.setLayout(null);
        JLabel jLabel2=new JLabel("本系统学生端功能如下：");
        jLabel2.setBounds(10,0,700,40);
        jLabel2.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel3=new JLabel("1.在线测评：输入c/c++,java代码即可在线编译运行得出结果");
        jLabel3.setBounds(10,40,900,40);
        jLabel3.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel4=new JLabel("2.远程操控：学生端输入教师IP地址后即可连接，可将页面传输到教师端，同时");
        jLabel4.setBounds(10,80,1000,40);
        jLabel4.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel5=new JLabel("进行语音交流");
        jLabel5.setBounds(10,120,1000,40);
        jLabel5.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel6=new JLabel("3.聊天：首先输入教师IP地址和自己的昵称，然后可以进行文字交流和文件传输");
        jLabel6.setBounds(10,160,1000,40);
        jLabel6.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel7=new JLabel("(1)文字交流：学生端将文字发送给教师端同时接受教师端发送的信息");
        jLabel7.setBounds(10,200,1000,40);
        jLabel7.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel8=new JLabel("(2)文件传输：将本地文件发送给教师端同时接受教师端发送的文件");
        jLabel8.setBounds(10,240,1000,40);
        jLabel8.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel9=new JLabel("4.OJ系统");
        jLabel9.setBounds(10,280,1000,40);
        jLabel9.setFont(new Font("楷体",Font.BOLD,25));
        JLabel jLabel10=new JLabel("学生端可以查看题目，完成题目以及测评结果展示，支持c/c++,java等语言");
        jLabel10.setBounds(10,320,1000,40);
        jLabel10.setFont(new Font("楷体",Font.BOLD,25));
        panel6.add(jLabel2);
        panel6.add(jLabel3);
        panel6.add(jLabel4);
        panel6.add(jLabel5);
        panel6.add(jLabel6);
        panel6.add(jLabel7);
        panel6.add(jLabel8);
        panel6.add(jLabel9);
        panel6.add(jLabel10);
        ImageIcon imageIcon2 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\在线测评.png");
        imageIcon2.setImage(imageIcon2.getImage().getScaledInstance(16,16,Image.SCALE_DEFAULT));
        tabbedPane.addTab("在线测评",imageIcon2,panel1);
        ImageIcon imageIcon1 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\远程操控.png");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(16,16,Image.SCALE_DEFAULT));
        tabbedPane.addTab("远程操控",imageIcon1,panel2);
        ImageIcon imageIcon3 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\对话.png");
        imageIcon3.setImage(imageIcon3.getImage().getScaledInstance(17,17,Image.SCALE_DEFAULT));
        tabbedPane.addTab("聊天",imageIcon3,panel3);
        ImageIcon imageIcon4 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\在线测评 (1).png");
        imageIcon4.setImage(imageIcon4.getImage().getScaledInstance(17,17,Image.SCALE_DEFAULT));
        tabbedPane.addTab("OJ系统",imageIcon4,panelOJ);
        ImageIcon imageIcon5 = new ImageIcon("src\\main\\java\\org\\ftz\\images\\帮助与说明.png");
        imageIcon5.setImage(imageIcon5.getImage().getScaledInstance(18,18,Image.SCALE_DEFAULT));
        tabbedPane.addTab("说明",imageIcon5,panel6);
        c.add(tabbedPane);
        c.setBackground(Color.white);
        jf.setVisible(true);
    }
    public void insertStatus(Status status) throws UnknownHostException { //插入一条做题的状态
        Connection connection = null;
        PreparedStatement statement =null;
        String ip= InetAddress.getLocalHost().getHostAddress();
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "insert into oj_status values(?,?,?,?,?)"; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setString(1,status.getIp());
            statement.setInt(2,status.getId());
            statement.setString(3,status.getStatus());
            statement.setString(4,status.getLanguage());
            long time=status.getTime().getTime();
            statement.setTimestamp(5,new java.sql.Timestamp(time) );
            int ret = statement.executeUpdate();  // 3. 执行 SQL 语句
            if(ret == 1){
                System.out.println("插入成功!");
            }else {
                System.out.println("插入失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    public class Page{ //分页显示
        public int currentPage=1;
        public int pageSize=10;
        public int lastPage;
        public int getCurrentPage() {
            return currentPage;
        }
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
        public int getPageSize() {
            return pageSize;
        }
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
        public int getLastPage() {
            return lastPage;
        }
        public void setLastPage(int lastPage) {
            this.lastPage = lastPage;
        }
    }
    public void showTable(int currentPage){ //分页展示
        model.setRowCount(0);
        page.setCurrentPage(currentPage);
        int offset=(currentPage-1)*page.getPageSize();
        int rows=page.getPageSize();
        List<StuShow> stuShows = clientshow(offset, rows);
        for(int row=0;row<stuShows.size();row++){
            Vector vector=new Vector();
            StuShow stuShow = stuShows.get(row);
            Problem problem = stuShow.getProblem();
            vector.add(problem.getId());
            vector.add(problem.getTitle());
            vector.add(problem.getLevel());
            vector.add(stuShow.getCount());
            vector.add(stuShow.getAcrate());
            model.addRow(vector);
        }
    }
    public List<Status> selectSta(int a, int b){ //分页查询状态
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Status> list = new ArrayList<>();
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "select ip,id,status,language,time from oj_status ORDER BY time DESC limit ?,? "; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setInt(1,a);
            statement.setInt(2,b);
            resultSet = statement.executeQuery();  // 3. 执行 SQL 语句
            while (resultSet.next()){   // 4. 遍历结果集
                Status status = new Status();
                status.setIp(resultSet.getString(1));
                status.setId(resultSet.getInt(2));
                status.setStatus(resultSet.getString(3));
                status.setLanguage(resultSet.getString(4));
                Timestamp timestamp = resultSet.getTimestamp(5);
                String s = String.valueOf(timestamp);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date=simpleDateFormat.parse(s.split("\\.")[0]);
                status.setTime(date);
                list.add(status);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        return list;
    }
    public List<Status> selectStaByIp(int a, int b){ //查询某个ip的做题状态
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Status> list = new ArrayList<>();
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "select ip,id,status,language,time from oj_status where ip=? ORDER BY time DESC limit ?,? "; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setString(1,InetAddress.getLocalHost().getHostAddress());
            statement.setInt(2,a);
            statement.setInt(3,b);
            resultSet = statement.executeQuery();  // 3. 执行 SQL 语句
            while (resultSet.next()){  // 4. 遍历结果集
                Status status = new Status();
                status.setIp(resultSet.getString(1));
                status.setId(resultSet.getInt(2));
                status.setStatus(resultSet.getString(3));
                status.setLanguage(resultSet.getString(4));
                Timestamp timestamp = resultSet.getTimestamp(5);
                String s = String.valueOf(timestamp);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date=simpleDateFormat.parse(s.split("\\.")[0]);
                status.setTime(date);
                list.add(status);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);  // 5. 关闭释放资源
        }
        return list;
    }
    public void showTable2(int currentPage){ //分页显示
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model2.setRowCount(0);
        page2.setCurrentPage(currentPage);
        int offset=(currentPage-1)*page2.getPageSize();
        int rows=page2.getPageSize();
        List<Status> statuses = selectSta(offset, rows);
        for(int row=0;row<statuses.size();row++){
            Vector vector=new Vector();
            Status status = statuses.get(row);
            vector.add(simpleDateFormat.format(status.getTime()));
            vector.add(status.getIp());
            vector.add(status.getStatus());
            vector.add(status.getId());
            vector.add(status.getLanguage());
            model2.addRow(vector);
        }
    }
    public void showTable3(int currentPage){ //分页显示
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model2.setRowCount(0);
        page3.setCurrentPage(currentPage);
        int offset=(currentPage-1)*page3.getPageSize();
        int rows=page3.getPageSize();
        List<Status> statuses = selectStaByIp(offset, rows);
        System.out.println(statuses);
        for(int row=0;row<statuses.size();row++){
            Vector vector=new Vector();
            Status status = statuses.get(row);
            vector.add(simpleDateFormat.format(status.getTime()));
            vector.add(status.getIp());
            vector.add(status.getStatus());
            vector.add(status.getId());
            vector.add(status.getLanguage());
            model2.addRow(vector);
        }
    }
    public List<Problem> selectLimit(int a,int b){ //分页查询问题
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Problem> list = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();  // 1. 建立连接
            String sql = "select id,title,level from oj_problem limit ?,?"; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setInt(1,a);
            statement.setInt(2,b);
            resultSet = statement.executeQuery(); // 3. 执行 SQL 语句
            while (resultSet.next()){// 4. 遍历结果集
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                list.add(problem);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        return list;
    }
    public List<Integer> selectCount(int a,int b){ //查询做题状态数量
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Integer> count = new ArrayList<>();
        List<Problem> problems = selectLimit(a, b);
        try {
            connection = DBUtil.getConnection();
            String sql2="select count(*) from oj_status WHERE id=?";
            for(int i=0;i<problems.size();i++){
                statement = connection.prepareStatement(sql2);
                statement.setInt(1,problems.get(i).getId());
                resultSet=statement.executeQuery();
                if(resultSet.next()){
                    count.add(resultSet.getInt(1));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);  // 5. 关闭释放资源
        }
        return count;
    }
    public List<Integer> selectaccnt(int a,int b){ //查询每个题目AC的个数
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Integer> count = new ArrayList<>();
        List<Problem> problems = selectLimit(a, b);
        try {
            connection = DBUtil.getConnection();
            String sql2="select count(*) from oj_status WHERE id=? and status='Accepted'";
            for(int i=0;i<problems.size();i++){
                statement = connection.prepareStatement(sql2);
                statement.setInt(1,problems.get(i).getId());
                resultSet=statement.executeQuery();
                if(resultSet.next()){
                    count.add(resultSet.getInt(1));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        return count;
    }
    public List<StuShow> clientshow(int a, int b){ //客户端展示题目
        List<StuShow> stuShows=new ArrayList<>();
        DecimalFormat decimalFormat=new DecimalFormat("#.##%");
        List<Problem> problems = selectLimit(a, b);
        List<Integer> count = selectCount(a, b);
        List<Integer> selectaccnt = selectaccnt(a, b);
        for(int i=0;i<problems.size();i++){
            StuShow stuShow = new StuShow();
            stuShow.setProblem(problems.get(i));
            stuShow.setCount(count.get(i));
            stuShow.setAccount(selectaccnt.get(i));
            if(stuShow.getCount()==0){
                stuShow.setAcrate("0%");
            }else {
                double x=(stuShow.getAccount()*1.0)/stuShow.getCount();
                String s = decimalFormat.format(x);
                stuShow.setAcrate(s);
            }
            stuShows.add(stuShow);
        }
        return stuShows;
    }
    public int getColumn(){ //题目数量
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int rowCount=0;
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "select count(*) from oj_problem"; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();  // 3. 执行 SQL 语句
            if(resultSet.next()){ // 4. 遍历结果集
                rowCount=resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        System.out.println(rowCount);
        return rowCount;
    }
    public int getColumn2(){ //题目状态数量
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int rowCount=0;
        try {
            connection = DBUtil.getConnection();  // 1. 建立连接
            String sql = "select count(*) from oj_status"; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery(); // 3. 执行 SQL 语句
            if(resultSet.next()){  // 4. 遍历结果集
                rowCount=resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);  // 5. 关闭释放资源
        }
        System.out.println(rowCount);
        return rowCount;
    }
    public int getColumn3(){ //某个ip题目状态数量
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int rowCount=0;
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "select count(*) from oj_status where ip=?"; // 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setString(1,InetAddress.getLocalHost().getHostAddress());
            resultSet = statement.executeQuery();// 3. 执行 SQL 语句
            if(resultSet.next()){ // 4. 遍历结果集
                rowCount=resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        System.out.println(rowCount);
        return rowCount;
    }
    public Problem selectBId(int id){ //根据题目编号查询题目
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Problem problem = new Problem();
        try {
            connection = DBUtil.getConnection(); // 1. 建立连接
            String sql = "select * from oj_problem where id=?";// 2. 拼装 SQL 语句
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery(); // 3. 执行 SQL 语句
            while (resultSet.next()){ // 4. 遍历结果集
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTestcasepath(resultSet.getString("testcasepath"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet); // 5. 关闭释放资源
        }
        return problem;
    }
}
class TableCellRendererButton implements TableCellRenderer{
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JPanel panel=new JPanel(null);
        JLabel label=new JLabel();
        label.setFont(new Font("微软雅黑",Font.BOLD,15));
        label.setForeground(Color.white);
        if(table.getValueAt(row,column).equals("Accepted")){
            label.setText("Accepted");
            label.setOpaque(true);
            label.setBackground(new Color(25,190,107));
            label.setBounds(0,7,84,25);
        }else if(table.getValueAt(row, column).equals("Wrong Answer")){
            label.setText("Wrong Answer");
            label.setOpaque(true);
            label.setBackground(new Color(237,63,20));
            label.setBounds(0,7,125,25);
        }
        else if(table.getValueAt(row, column).equals("Compile Error") ) {
            label.setText("Compile Error");
            label.setOpaque(true);
            label.setBackground(new Color(255,153,0));
            label.setBounds(0,7,116,25);
        }
        Border border = label.getBorder();
        Border emptyBorder = new EmptyBorder(10, 6, 10, 6);
        label.setBorder(new CompoundBorder(border,emptyBorder));
        panel.add(label);
        return panel;
    }
}
